import axios from "axios";
import toastr from "toastr";

// const BASE_URL = "http://192.168.31.55:8080";
const BASE_URL = "http://192.168.31.118:8080";
// const BASE_URL = "http://47.113.227.183:8080";

export default {

    // 发布文章
    async articleIssue(obj) {
        return await axios({
            method: 'post',
            url: BASE_URL + '/article/issue',
            // headers: { 'content-type': 'multipart/form-data' },
            data: obj,
        }).then((res) => {
            if (res.data == "1") {
                return res.data;
            }
            toastr.error("也许重试可以解决,保存好数据！", "错误!", { timeOut: 2000 });
        })
    },


    // 发布动态
    async dynamicIssue(obj, path, handleUploadProgress) {
        return await axios({
            method: 'post',
            url: BASE_URL + '/dynamic/' + path,
            // headers: { 'content-type': 'multipart/form-data' },
            data: obj,
            onUploadProgress: handleUploadProgress
        }).then((res) => {
            if (res.data == "success") {
                localStorage.removeItem("dynamicIssue");
                toastr.success("发送成功", '', { timeOut: 2000 });
                return true;
            }
            toastr.error("发送失败,请重试", "错误!", { timeOut: 2000 });
        })
    },

    //首页获取文章
    async getArticle(page) {
        return await axios
            .get(BASE_URL + "/article/list", {
                params: {
                    page: page,
                }
            })
            .then((res) => {
                return res.data;
            })
            .catch((err) => {
                return err;
            })
    },

    //首页获取动态
    async getDynamic(page) {
        return await axios
            .get(BASE_URL + "/dynamic/list", {
                params: {
                    page: page,
                }
            })
            .then((res) => {
                return res.data;
            })
            .catch((err) => {
                return err;
            })
    },

    //获取id用户的动态数据
    async getUserDynamic(page, id) {
        return await axios
            .get(BASE_URL + "/dynamic/user", {
                params: {
                    page: page,
                    id: id
                }
            })
            .then((res) => {
                return res.data;
            })
    },

    //获取id用户评论
    async getUserComment(page, id) {
        return await axios
            .get(BASE_URL + "/comment/user", {
                params: {
                    page: page,
                    id: id
                }
            })
            .then((res) => {
                return res.data;
            })
    },

    //获取实体页面最近评论
    async getRecentComment() {
        // console.log("1111");
        return await axios
            .get(BASE_URL + "/comment/all_10")
            .then((res) => {
                return res.data;
            })
    },

    //获取评论或动态实体内容
    async getEntity(id, type) {
        if (type == "article") {
            return await axios
                .get(BASE_URL + "/article/one", {
                    params: {
                        id: id
                    },
                })
                .then((res) => {
                    return res.data;
                })
        }
        if (type == "dynamic") {
            return await axios
                .get(BASE_URL + "/dynamic/one", {
                    params: {
                        id: id
                    },
                })
                .then((res) => {
                    return res.data;
                })
        }
        else {
            return await axios
                .get(BASE_URL + "/comment/one", {
                    params: {
                        id: id
                    },
                })
                .then((res) => {
                    return res.data;
                })
        }
    },

    //获取用户消息队列 0未读 1已读 -1全部
    async getUserMessage(page, id, state) {
        return await axios
            .get(BASE_URL + "/message/user", {
                params: {
                    page: page,
                    id: id,
                    state: state
                }
            })
            .then((res) => {
                return res.data;
            })
    },

    //使其全部已读
    async readAll(id) {
        return await axios
            .get(BASE_URL + "/message/read", {
                params: {
                    id: id
                }
            })
            .then((res) => {
                return res.data;
            })
    },

    //获取文章、动态、评论的评论列表 1升序 0降序
    async getEntityCommentList(page, id, order) {
        return await axios
            .get(BASE_URL + "/comment/list", {
                params: {
                    page: page,
                    id: id,
                    order: order
                },
            })
            .then((res) => {
                return res.data
            })
    },

    //发布评论 
    async commentIssue(obj) {
        return await axios({
            method: 'post',
            url: BASE_URL + '/comment/add',
            headers: { 'content-type': 'application/json' },
            data: obj,
        }).then((res) => {
            if (res.data == "1") {
                toastr.success("发送成功", '', { timeOut: 2000 });
                return true;
            }
            toastr.error("发送失败,请重试", "错误!", { timeOut: 3000 });
        })
    },


    //查询是否点赞
    async isLike(id) {
        return await axios
            .get(BASE_URL + "/like/isLike", {
                params: {
                    entityId: id,
                },
            })
            .then((res) => {
                if (res.data == "1") {
                    toastr.success("点赞成功", '', { timeOut: 1000 });
                }
                return res.data
            })
    },

    //点赞取消点赞
    async toLike(id, type) {
        return await axios
            .get(BASE_URL + "/like/add", {
                params: {
                    entityId: id,
                    type: type,
                },
            })
            .then((res) => {
                return res.data
            })
    },

    //发布番评
    async criticismIssue(obj) {
        // console.log(obj);
        return await axios({
            method: 'post',
            url: BASE_URL + '/criticism/add',
            headers: { 'content-type': 'application/json' },
            data: obj,
        }).then((res) => {
            if (res.data == "1") {
                toastr.success("发送成功", '', { timeOut: 2000 });
                return true;
            }
            toastr.error("发送失败,请重试", "错误!", { timeOut: 3000 });
        })
    },

    //获取该番剧番评列表
    async getCriticismList(page, id, order) {
        return await axios
            .get(BASE_URL + "/criticism/list", {
                params: {
                    page: page,
                    id: id,
                    order: order,
                },
            })
            .then((res) => {
                return res.data
            })
    },

    //获取用户番评
    async getUserCriticism(id, page) {
        return await axios
            .get(BASE_URL + "/criticism/user", {
                params: {
                    page: page,
                    id: id,
                },
            })
            .then((res) => {
                return res.data
            })
    },

    //搜索
    async search(text, page, path) {
        return await axios
            .get(BASE_URL + "/search/" + path, {
                params: {
                    text: text,
                    page: page,
                },
            })
            .then((res) => {
                return res.data
            })
    }

}